package com.education.admin.api.controller.system;

import com.education.common.base.BaseController;
import com.education.common.utils.MapTreeUtils;
import com.education.common.utils.Result;
import com.education.service.system.SystemMenuService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 余一
 * @Description
 * @date 2021/8/11 17:04
 */
@RestController
@RequestMapping("/system/menu")
public class SystemMenuController extends BaseController {

    @Autowired
    private SystemMenuService systemMenuService;

    /**
     * 获取菜单列表
     * @return
     */
    @GetMapping("getMenuList")
    @RequiresPermissions("system:menu:list")
    public Result getMenuList(){
        Result result = systemMenuService.pagination(new HashMap());
        Map dataMap = result.getData();
        List<Map> treeList = MapTreeUtils.buildTreeData((List<Map>) dataMap.get("dataList"));
        dataMap.put("dataList",treeList);
        return result;
    }

    /**
     * 保存或修改菜单
     * @param menuMap
     * @return
     */
    @PostMapping
    @RequiresPermissions(value = {"system:menu:save", "system:menu:update"}, logical = Logical.OR)
    public Result saveOrUpdateMenu(@RequestBody Map menuMap){
        return systemMenuService.saveOrUpdate(menuMap);
    }

    /**
     * 根据id查询菜单
     * @param menuId
     * @return
     */
    @GetMapping("findById")
    public Result findById(Integer menuId){
      return systemMenuService.findById(menuId);
    }

    /**
     * 根据id删除权限菜单
     * @param menuMap
     * @return
     */
    @DeleteMapping
    @RequiresPermissions("system:menu:deleteById")
    public Result deleteById(@RequestBody Map menuMap){
        return systemMenuService.deleteById(menuMap);
    }

    /**
     * 根据角色id获取相应权限资源
     * @param roleId
     * @return
     */
    @GetMapping("/getRolePermission")
    public Result getRolePermission(Integer roleId){
        return Result.success(systemMenuService.getRolePermission(roleId));
    }


}
